package LINE.comm.interceptor;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import LINE.ACT.mind.home.service.homeService;


/**
 * @author devk
 *
 */
public class WeblogInterceptor extends HandlerInterceptorAdapter {
		
	@Resource(name="HOME_SERVICE")
	private homeService homeservice; 
	
	@Override
	 public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
	                  Object hadler) throws Exception {
		
		//Write DB Logging
		if(request.getRequestURL().indexOf("view") >=0)
		{
			Object userName = request.getSession().getAttribute("USER_NAME");
			
			if(userName != null){
				Map commandMap = new java.util.HashMap();
				commandMap.put("USER_NAME", userName);
				homeservice.updateNewScreenCnt(commandMap, request);
				Object seqNo = null;
				
				if(request.getRequestURL().indexOf("blogpost") >=0){
					
					
					Object blogNo = request.getParameter("BLOG_NO");
					commandMap.put("BLOG_NO", blogNo);
					
					Map cnt = (Map<String, Object>)homeservice.selectBlogCntForIdentify(commandMap, request);
					
					request.getSession().setAttribute("POST_YN", "N");
					if(!"0".equals(cnt.get("CNT").toString())){
						request.getSession().setAttribute("POST_YN", "Y");
						seqNo = homeservice.insertUserStayPeriodbyUser(commandMap, request);
						request.getSession().setAttribute("SEQ_NO", seqNo);
					}
				}
				
				Object post = request.getSession().getAttribute("POST_YN");
				if(request.getRequestURL().indexOf("blogpost") <= 0){
					if(post != null){
						if("Y".equals(post)){
							commandMap.put("SEQ_NO", request.getSession().getAttribute("SEQ_NO"));
							homeservice.updateUserStayPeriodbyUser(commandMap, request);
							homeservice.updateUserStayTime(commandMap, request);
							request.getSession().setAttribute("POST_YN", "N");
						}
					}
				}
				
			}
			

		}
		
		return true;
	}
	
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler, ModelAndView modeAndView) throws Exception {
		
		
	}
	
}
